﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamir.IPLib.Packets;
using System.Net;
using System.IO;

namespace HostDetector
{
    class PacketAnalyser
    {
        PacketSniffer ps;
        
        public delegate void DataPacketHandler(IPEndPoint src, IPEndPoint dst,byte[] data);
        public event DataPacketHandler Big6Event;

        public delegate void PingMessageHandler(IPEndPoint ip, byte[] message);
        public event PingMessageHandler  PingMessageEvent;



        public PacketAnalyser()
        {
            ps = new PacketSniffer();
            ps.connect();
            ps.packetEvent += new PacketSniffer.packetDel(ps_packetEvent);
        }

        IPEndPoint getSrcEndPoint(UDPPacket packet)
        {
            return new IPEndPoint(System.Net.IPAddress.Parse(packet.SourceAddress), packet.SourcePort);
        }
        IPEndPoint getDstEndPoint(UDPPacket packet)
        {
            return new IPEndPoint(System.Net.IPAddress.Parse(packet.DestinationAddress), packet.DestinationPort);
        }
        void ps_packetEvent(UDPPacket packet)
        {




            if (packet.Data[0] == 0x0d && packet.Data[1] == 0x02)
            {
                if (packet.Data.Length == 29)
                {
                    PingMessageEvent(getDstEndPoint(packet), packet.Data);
                }

            }
            
            
            else if (packet.Data[0] == 6 && packet.Data[1] == 2)
            {

                Big6Event(getSrcEndPoint(packet), getDstEndPoint(packet), packet.Data);
            }

            
            
            
        }



    }
}
